package com.delio.client.contabilidad.proveedores;

import com.delio.client.idioma.DelioConstants;
import com.delio.client.pagos.tablasbasicas.Tipos;
import com.delio.client.util.ComboboxFieldBinding;
import com.delio.client.util.Utilities;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.binding.FieldBinding;
import com.extjs.gxt.ui.client.binding.FormBinding;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.util.IconHelper;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.ComboBox;
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;
import com.extjs.gxt.ui.client.widget.form.FormButtonBinding;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.HiddenField;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.google.gwt.core.client.GWT;

public class PanelNuevoProveedor extends FormPanel {
	private DelioConstants k = GWT.create(DelioConstants.class);
	private FormBinding formbinding;
	private HiddenField<String> id;
	private Button btnRegistrar;
	private FormButtonBinding formbuttonbinding;
	
	private TextField<String> txtDni;
	private TextField<String> txtRuc;
	private TextField<String> txtRazonsocial;
	private TextField<String> txtNombrecomercial;
	private TextField<String> txtRepresentante;
	private TextField<String> txtDireccion;
	private TextField<String> txtEmail;
	private TextField<String> txtWeb;
	private ComboBox<BeanModel> cmbTipoproveedor;
	

	public PanelNuevoProveedor() {
		super();
		setScrollMode(Scroll.AUTOY);
		
		setHeading(k.nuevoProveedor());
		setFrame(true);
		setLabelWidth(120);
		setLabelAlign(LabelAlign.TOP);

		
        PanelGridProveedor.panelnuevoproveedor=this;
		formbinding=new FormBinding(this);
		
		btnRegistrar = new Button(k.registrar(),IconHelper.createPath("images/icons/save_all.png"));
		btnRegistrar.setSize(80, 30);
		btnRegistrar.addSelectionListener(new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				save();
			}
		});
		
		formbuttonbinding = new FormButtonBinding(this);
		formbuttonbinding.addButton(btnRegistrar);

		
		Tipos.getList("listProveedor");
		Tipos.loader.load();

		cmbTipoproveedor = new ComboBox<BeanModel>();
		cmbTipoproveedor.setFieldLabel(k.tipoProveedor());
		cmbTipoproveedor.setEmptyText(k.comboempty());
		cmbTipoproveedor.setEditable(false);
		cmbTipoproveedor.setDisplayField("tipo");
		cmbTipoproveedor.setValueField("tipo");
		cmbTipoproveedor.setStore(Tipos.store);
		cmbTipoproveedor.setTriggerAction(TriggerAction.ALL);
		cmbTipoproveedor.setValueField("tipo");
		cmbTipoproveedor.setAllowBlank(false);
		cmbTipoproveedor.setUseQueryCache(false);
		
		id = new HiddenField<String>();
		

		txtRuc = new TextField<String>();
		txtRuc.setFieldLabel(k.ruc());
		
		
		
		txtDni = new TextField<String>();
		txtDni.setFieldLabel(k.dni());
		txtDni.setMinLength(8);
		txtDni.focus();
		
		Utilities.setNumerosEnteros(txtRuc);
		Utilities.setLengh(txtRuc, txtDni, 11);
		
		txtRazonsocial = new TextField<String>();
		txtRazonsocial.setFieldLabel(k.razonSocial());
		txtRazonsocial.setAllowBlank(false);
		Utilities.setNumerosEnteros(txtDni);
		Utilities.setLengh(txtDni, txtRazonsocial, 8);
		

		txtRepresentante = new TextField<String>();
		txtRepresentante.setFieldLabel(k.representante());
		txtRepresentante.setAllowBlank(false);

		txtNombrecomercial = new TextField<String>();
		txtNombrecomercial.setFieldLabel(k.nombreComercial());
		
		txtDireccion = new TextField<String>();
		txtDireccion.setFieldLabel(k.direccioncliente());
		
		txtEmail = new TextField<String>();
		txtEmail.setFieldLabel(k.email());
		Utilities.validarEmail(txtEmail);
		
		txtWeb=new TextField<String>();
		txtWeb.setFieldLabel(k.web());
		
	
		add(id);
		add(cmbTipoproveedor, new FormData("95%"));
		add(txtRuc, new FormData("70%"));
		add(txtDni, new FormData("70%"));
		add(txtRazonsocial, new FormData("95%"));
		add(txtRepresentante, new FormData("95%"));
		add(txtNombrecomercial, new FormData("95%"));
		add(txtEmail, new FormData("70%"));
		add(txtDireccion, new FormData("95%"));
		add(txtWeb, new FormData("95%"));
		
		addButton(btnRegistrar);
		setButtonAlign(HorizontalAlignment.CENTER);
		
		
		setStore(Proveedores.store);
	}
	
	
	public void setStore(ListStore<BeanModel> store) {

		formbinding.addFieldBinding(new FieldBinding(id, "id"));
		formbinding.addFieldBinding(new FieldBinding(txtDni, "dni"));
		formbinding.addFieldBinding(new FieldBinding(txtRuc, "ruc"));
		formbinding.addFieldBinding(new FieldBinding(txtRazonsocial, "razonsocial"));
		formbinding.addFieldBinding(new FieldBinding(txtRepresentante, "representante"));
		formbinding.addFieldBinding(new FieldBinding(txtNombrecomercial, "nombrecomercial"));
		formbinding	.addFieldBinding(new FieldBinding(txtEmail, "email"));
		formbinding.addFieldBinding(new FieldBinding(txtDireccion, "direccion"));
		formbinding.addFieldBinding(new FieldBinding(txtWeb, "web"));
		formbinding.addFieldBinding(new ComboboxFieldBinding(cmbTipoproveedor,"tipoproveedor"));
		formbinding.setStore(store);
		
	}

	@Override
	public void reset() {
		id.clear();
		txtDni.clear();
		txtRuc.clear();
		txtRazonsocial.clear();
		txtRepresentante.clear();
		txtNombrecomercial.clear();
		txtDireccion.clear();
		txtEmail.clear();
		txtWeb.clear();
		
		PanelGridProveedor.grid.getSelectionModel().deselectAll();
		unbind();

	}

	public void bind(BeanModel selectedModel) {
		formbinding.bind(selectedModel);
		setHeading(k.ModificarEmpleado());

	}

	public void unbind() {
		formbinding.unbind();
		setHeading(k.nuevoProveedor());
	}
	
	public void save(){
		Integer proveedor_id;
		
		Integer tipoproveedor_id = Integer.valueOf(cmbTipoproveedor.getValue()
				.get("id").toString());
		String dni=txtDni.getValue();
		String ruc=txtRuc.getValue();
		String razonsocial=txtRazonsocial.getValue();
		String nombrecomercial=txtNombrecomercial.getValue();
		String representante=txtRepresentante.getValue();
		String direccion=txtDireccion.getValue();
		String email=txtEmail.getValue();
		String web=txtWeb.getValue();
		
		Proveedor pro=new Proveedor();
		
	
		
		if(id.getValue()!=null){
			proveedor_id=Integer.valueOf(id.getValue().toString());
		
			pro.setId(proveedor_id);
			}
		
		pro.setTipoproveedor_id(tipoproveedor_id);
		pro.setDni(dni);
		pro.setRuc(ruc);
		pro.setRazonsocial(razonsocial);
		pro.setNombrecomercial(nombrecomercial);
		pro.setRepresentante(representante);
		pro.setDireccion(direccion);
		pro.setWeb(web);
		pro.setEmail(email);
		
		Proveedores.save(pro);
		Proveedores.panelnuevoproveedor=this;
		
	}
}
